package com.ruoyi.erp.service.impl;

import java.util.Date;
import java.util.List;

import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.erp.domain.Project;
import com.ruoyi.erp.domain.ProjectMember;
import com.ruoyi.erp.domain.StaffingSetting;
import com.ruoyi.erp.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.erp.mapper.ProjectMemberMapper;
import com.ruoyi.common.core.text.Convert;
import org.springframework.transaction.annotation.Transactional;

/**
 * 项目成员/外派人员Service业务层处理
 * 
 * @author sauger
 * @date 2022-03-15
 */
@Service
public class ProjectMemberServiceImpl implements IProjectMemberService 
{
    @Autowired
    private ProjectMemberMapper projectMemberMapper;

    @Autowired
    private IProjectService projectService;

    @Autowired
    private IStaffingSettingService staffingSettingService;

    @Autowired
    private IHolidayConfigService holidayConfigService;

    @Autowired
    private IStaffingMonthBillService monthBillService;

    /**
     * 查询项目成员/外派人员
     * 
     * @param id 项目成员/外派人员主键
     * @return 项目成员/外派人员
     */
    @Override
    public ProjectMember selectProjectMemberById(Integer id)
    {
        return projectMemberMapper.selectProjectMemberById(id);
    }

    /**
     * 查询项目成员/外派人员列表
     * 
     * @param projectMember 项目成员/外派人员
     * @return 项目成员/外派人员
     */
    @Override
    public List<ProjectMember> selectProjectMemberList(ProjectMember projectMember)
    {
        return projectMemberMapper.selectProjectMemberList(projectMember);
    }

    /**
     * 新增项目成员/外派人员
     * 
     * @param projectMember 项目成员/外派人员
     * @return 结果
     */
    @Override
    @Transactional
    public int insertProjectMember(ProjectMember projectMember)
    {
        projectMember.setCreateTime(DateUtils.getNowDate());
        Project project = projectService.selectProjectById(projectMember.getProjectId());
        if (StringUtils.isNull(project)) throw new ServiceException("参数错误");
        ProjectMember search = new ProjectMember();
        if (project.getProjectType().equals("staffing")){
            //加入其他外派项目的人，不能重复加入

            search.setUserId(projectMember.getUserId());
            search.setOnboardStatus("onboard");
            Project searchProject = new Project();
            searchProject.setProjectType("staffing");
            search.setProject(searchProject);
            List<ProjectMember> exist = this.selectProjectMemberList(search);
            if (!StringUtils.isNull(exist) && !exist.isEmpty()){
                throw new ServiceException("该员工已在外派项目 " + exist.get(0).getProject().getShortTitle()+ " 中");
            }
        }else
        {
            //普通项目，判断同一个人不能重复加入
            search.setProjectId(projectMember.getProjectId());
            search.setOnboardStatus("onboard");
            search.setUserId(projectMember.getUserId());
            List<ProjectMember> exist = this.selectProjectMemberList(search);
            if (!StringUtils.isNull(exist) && !exist.isEmpty()){
                throw new ServiceException("该员工已在项目中！");
            }
        }
        projectMember.setOnboardStatus("onboard");
        projectMemberMapper.insertProjectMember(projectMember);

        StaffingSetting staffingSetting = staffingSettingService.selectStaffingSettingByResourceWithDefault("project", projectMember.getProjectId());

        staffingSetting.setResourceId(projectMember.getId());
        staffingSetting.setResourceType("person");
        staffingSetting.setId(null);
        staffingSetting.setQuotation(projectMember.getStaffingSetting().getQuotation());
        staffingSetting.setQuotationType(projectMember.getStaffingSetting().getQuotationType());
        staffingSetting.setResourceId(projectMember.getId());
        return StringUtils.isNull(staffingSetting.getId()) || staffingSetting.getId() == 0 ? staffingSettingService.insertStaffingSetting(staffingSetting) : staffingSettingService.updateStaffingSetting(staffingSetting);

    }

    /**
     * 修改项目成员/外派人员
     * 
     * @param projectMember 项目成员/外派人员
     * @return 结果
     */
    @Override
    public int updateProjectMember(ProjectMember projectMember)
    {
        projectMember.setUpdateTime(DateUtils.getNowDate());
        return projectMemberMapper.updateProjectMember(projectMember);
    }

    /**
     * 批量删除项目成员/外派人员
     * 
     * @param ids 需要删除的项目成员/外派人员主键
     * @return 结果
     */
    @Override
    public int deleteProjectMemberByIds(String ids)
    {
        return projectMemberMapper.deleteProjectMemberByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除项目成员/外派人员信息
     * 
     * @param id 项目成员/外派人员主键
     * @return 结果
     */
    @Override
    public int deleteProjectMemberById(Integer id)
    {
        return projectMemberMapper.deleteProjectMemberById(id);
    }

    /**
     * 获取某月需要初始化账单的外派员工清单
     *
     * @param year
     * @param month
     * @return
     */
    @Override
    public List<ProjectMember> selectProjectMemeberNeedInitMonthBill(Integer year, Integer month) {
        Date headOfMonth = DateUtils.parseDate(year + "-" + month + "-1");
        Date tailOfMonth = DateUtils.addMonths(headOfMonth,1);

        return projectMemberMapper.selectProjectMembersNeedInitBill(year, month,headOfMonth.toString(), tailOfMonth.toString());
    }

//    @Override
//    public void calculateMonthBill(ProjectMember projectMember, Integer year, Integer month) {
//        //如果已经存在，则从数据中载入账单，否则新建
//        StaffingMonthBill search = new StaffingMonthBill();
//        search.setProjectId(projectMember.getProjectId());
//        search.setPersonId(projectMember.getId());
//        search.setBillYear(year);
//        search.setBillMonth(month);
//        List<StaffingMonthBill> exist = monthBillService.selectStaffingMonthBillList(search);
//        StaffingMonthBill bill;
//        if (StringUtils.isNull(exist) || exist.isEmpty()){
//            bill = new StaffingMonthBill();
//            bill.setBillMonth(month);
//            bill.setBillYear(year);
//            bill.setProjectId(projectMember.getProjectId());
//            bill.setPersonId(projectMember.getProjectId());
//        }else {
//            bill = exist.get(0);
//        }
//
//        StaffingSetting staffingSetting = staffingSettingService.selectStaffingSettingByResouce("person",projectMember.getId());
//        int workDay = holidayConfigService.getWorkdayCountByMonth(year, month, staffingSetting.getHolidayConfigType());
//        bill.setFullDayCount(BigDecimal.valueOf(workDay));
//
//        this.calculate(bill,staffingSetting);
//
//        if (StringUtils.isNull(bill.getId())){
//            monthBillService.insertStaffingMonthBill(bill);
//        }else{
//            monthBillService.updateStaffingMonthBill(bill);
//        }
//
//
//    }

//    @Override
//    public void calculateMonthBill(ProjectMember projectMember, Date date) {
//        Calendar calendar = Calendar.getInstance();
//        calendar.setTime(date);
//        Integer year = calendar.get(Calendar.YEAR);
//        Integer month = calendar.get(Calendar.MONTH);
//        this.calculateMonthBill(projectMember, year, month);
//    }





}
